//package wmmd.disc.webmaster;

import java.io.*;
//import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import db.DatabaseConnector;

public class ListMessages extends HttpServlet {
    
//This subroutine is called by ListMessages.jsp (admin page)
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        //The JSP document passes in which db table to use
        String tName = request.getParameter("tName");
        
        //Variables for the database connection
        DatabaseConnector con = null;
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        if(tName!=null) {
            try {
                con = new DatabaseConnector(DatabaseConnector.getDatabaseType());

                //TODO: Query
                String SQL = "SELECT ID, Name, Subject, Message FROM " + tName + " WHERE ID > 1 ORDER BY ID";
                String[][] rs = con.selectSQL(SQL,4);
                
                //Display the contents
                //while(rs.next()) {
                for (int i=0;i<rs.length;i++){
                    String idString = rs[i][0];
                    String nameString = rs[i][1];
                    String subjectString = rs[i][2];
                    String messageString = rs[i][3];
                    /*String idString = rs.getString("ID");
                    String nameString = rs.getString("Name");
                    String subjectString = rs.getString("Subject");
                    String messageString = rs.getString("Message");*/
                    
                    if(messageString.length() > 50) {
                        messageString = messageString.substring(0, 50) + "...";
                    }
                    
                    out.print("<tr><td>" + nameString);
                    out.print("</td><td> " + subjectString);
                    out.println("</td><td><a href=\"EditPost.jsp?ID=" + idString + "\"> " + messageString + "</a></td></tr>");
                }
            }
            finally
            {
                con.CleanUp();
            }
        }
    }
}
